/*
ont-space - The ontology-based resource metadata repository
Copyright (c) 2006-2008, Information Eng. Research Unit - Univ. of Alcalá
http://www.cc.uah.es/ie
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.ontspace.util.translator.sld2owl.owl;

import org.w3c.dom.DOMException;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.datatypes.RDFDatatype;
import com.hp.hpl.jena.datatypes.TypeMapper;

/**
 * This class translate the play element
 *
 */
public class PlayTranslator {

    /**
     * Creates a new instance of PlayTranslator    
     * @param Play The play class
     * @param lnode The play node of the Dom structure
     * @param ont The ontology
     * @param instMethod The method instance
     * @param nsName The namespace
     */
    public PlayTranslator(OntClass Play, Node lnode, OntModel ont,
            Individual instMethod, String nsName) {

        lastInstanceSLDTranslator lastInst =
                new lastInstanceSLDTranslator();


        DatatypeProperty title =
                ont.getDatatypeProperty(nsName + "title");
        DatatypeProperty identifier =
                ont.getDatatypeProperty(nsName + "identifier");
        DatatypeProperty isvisible =
                ont.getDatatypeProperty(nsName + "isvisible");

        OntClass Act =
                ont.getOntClass(nsName + "Act");

        String instance =
                "play_instance_" + (String.valueOf(lastInst.lastInstance(Play)));
        NodeList children =
                lnode.getChildNodes();

        Individual instLd =
                null;
        if (children != null) {
            for (int i =
                    0; i < children.getLength(); i++) {
                try {

                    instLd =
                            ont.createIndividual(nsName + instance, Play);

                    OntClass Method =
                            ont.getOntClass(nsName + "Method");
                    ObjectProperty playRef =
                            ont.getObjectProperty(nsName + "play-ref");
                    instMethod.setPropertyValue(playRef, instLd);


                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                if (children.item(i).getNodeName().compareTo("imsld:title") == 0) {
                    RDFNode titleValue =
                            ont.createTypedLiteral(children.item(i).
                            getTextContent());

                    try {
                        instLd.setPropertyValue(title, titleValue);

                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }

                if (children.item(i).getNodeName().compareTo("imsld:act") == 0) {
                    ActTranslator da =
                            new ActTranslator(Act, children.item(i), ont, instLd,
                            nsName);
                }

                if (children.item(i).getNodeName().compareTo("imsld:complete-play") ==
                        0) {
                    CompletePlayTranslator dcp =
                            new CompletePlayTranslator(children.item(i), ont,
                            instLd, nsName);
                }
                if (children.item(i).getNodeName().compareTo("on-completion") ==
                        0) {
                    NodeList feed =
                            children.item(i).getChildNodes();
                    for (int f =
                            0; f < feed.getLength(); f++) {
                        if (feed.item(f).getNodeName().
                                compareTo("imsld:feedback-description") == 0) {
                            FeedbackDescriptionTranslator dfd =
                                    new FeedbackDescriptionTranslator(feed.item(i),
                                    ont, instLd, Play, nsName);
                        }
                    }

                }
            }
            NamedNodeMap attrs =
                    lnode.getAttributes();
            for (int a =
                    0; a < attrs.getLength(); a++) {
                Node attr =
                        attrs.item(a);
                try {
                    if (attr.getNodeName().trim().compareTo("identifier") == 0) {
                        TypeMapper tm =
                                TypeMapper.getInstance();
                        RDFDatatype overType =
                                tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#ID");
                        RDFNode value =
                                ont.createTypedLiteral(attr.getNodeValue().trim(),
                                overType);
                        instLd.setPropertyValue(identifier, value);

                    }
                    if (attr.getNodeName().trim().compareTo("isvisible") == 0) {

                        RDFNode visibleValue;
                        if (attr.getNodeValue().compareTo("true") == 0) {
                            visibleValue =
                                    ont.createTypedLiteral(true);
                        } else {
                            visibleValue =
                                    ont.createTypedLiteral(false);
                        }

                        instLd.setPropertyValue(isvisible, visibleValue);


                    }
                } catch (DOMException ex) {
                    ex.printStackTrace();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

            }
        }
    }
    }   
         

       
        